//
//  SLAccessOneTimeVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/26.
//

#import "SLAccessOneTimeVC.h"
#import "SLFingerCreateVC.h"

@interface SLAccessOneTimeVC ()<DatePickerViewDelegate>{
    UIView *_dateBgView;
    NSDate *_startDate;
    NSString *_startDateStr;
    NSDate *_endDate;
    NSString *_endDateStr;
    NSString *_formart;
    NSString *_submitFormart;
}
@end 

@implementation SLAccessOneTimeVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"Make new access");
    _topLbl.text = kLocalString(@"Set an availability period");
    _formart = @"MMM. d, yyyy  HH:mm";
    _submitFormart = @"yyyy-MM-dd HH:mm:ss";
    _listContentLbl1.layer.cornerRadius = 10;
    _listContentLbl1.layer.masksToBounds = YES;
    _listContentLbl2.layer.cornerRadius = 10;
    _listContentLbl2.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    _sureBtn.btnStyle = 1;
    [_sureBtn setTitle:kLocalString(@"Next") forState:UIControlStateNormal];
    
    _listLbl1.text = kLocalString(@"From");
    _startDate = [NSDate date];
    _startDateStr = [_startDate stringWithFormat:_formart];
    _listContentLbl1.text = _startDateStr;
    
    _endDate = [[NSDate date] dateByAddingDays:1];
    _endDateStr = [_endDate stringWithFormat:_formart];
    _listContentLbl2.text = _endDateStr;
    
    _listLbl2.text = kLocalString(@"To");
    _listLbl0.text = kLocalString(@"One time duration");
    
    kWeakSelf(self);
    _listView1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:1];
    }];
    [_listView1 addGestureRecognizer:tap1];
    
    _listView2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:2];
    }];
    [_listView2 addGestureRecognizer:tap2];
    
}

-(void)handleListAction:(NSInteger)index{
    if (index == 1) {
        [self showDateViewWith:1];
    }else if (index == 2){
        [self showDateViewWith:2];
    }
}

-(void)showDateViewWith:(NSInteger)tag{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    _dateBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    _dateBgView.backgroundColor = [kHexColor(@"#0C0C0C") colorWithAlphaComponent:0.8];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_dateBgView];
    DatePickerView *datePickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight - 250, KScreenWidth, 250)];
    datePickerView.layer.cornerRadius = 10;
    datePickerView.layer.masksToBounds = YES;
    datePickerView.fundelegate = self;
    datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
    datePickerView.tag = tag;
    NSDate *now = [NSDate date];
    [datePickerView.datePicker setDate:now animated:YES];
    datePickerView.datePicker.frame = CGRectMake(0, 50, KScreenWidth, 200);
    [[[UIApplication sharedApplication] keyWindow] addSubview:datePickerView];
}

-(void)didDatePickerViewDoneClicked:(DatePickerView *)aPickerView{
   
    if (aPickerView.tag == 1) {
        _startDate = [aPickerView selectDate];
        _startDateStr = [_startDate stringWithFormat:_formart];
        _listContentLbl1.text = _startDateStr;
    }else{
        _endDate = [aPickerView selectDate];
        _endDateStr = [_endDate stringWithFormat:_formart];
        _listContentLbl2.text = _endDateStr;
    }
    [_dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

- (void)didDatePickerViewCancelClicked:(DatePickerView*)aPickerView{
    [_dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

- (IBAction)sureAction:(id)sender {
    _keyModel.startTime = [_startDate stringWithFormat:_submitFormart];
    _keyModel.endTime = [_endDate stringWithFormat:_submitFormart];;
    
    if (_keyModel.lockType == 1) {
        SLFingerCreateVC *vc = [[SLFingerCreateVC alloc] init];
        vc.keyModel = _keyModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [ZCTool uploadBlueToothKeyData:_keyModel];
    }
}

@end
